require("plenary.async").tests.add_to_env()
local oil = require("oil")
local test_util = require("tests.test_util")

a.describe("window options", function()
  after_each(function()
    test_util.reset_editor()
  end)

  a.it("Restores window options on close", function()
    vim.cmd.edit({ args = { "README.md" } })
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.equals("no", vim.o.signcolumn)
    oil.close()
    assert.equals("auto", vim.o.signcolumn)
  end)

  a.it("Restores window options on edit", function()
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.equals("no", vim.o.signcolumn)
    vim.cmd.edit({ args = { "README.md" } })
    assert.equals("auto", vim.o.signcolumn)
  end)

  a.it("Restores window options on split <filename>", function()
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.equals("no", vim.o.signcolumn)
    vim.cmd.split({ args = { "README.md" } })
    assert.equals("auto", vim.o.signcolumn)
  end)

  a.it("Restores window options on split", function()
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.equals("no", vim.o.signcolumn)
    vim.cmd.split()
    vim.cmd.edit({ args = { "README.md" } })
    assert.equals("auto", vim.o.signcolumn)
  end)

  a.it("Restores window options on tabnew <filename>", function()
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.equals("no", vim.o.signcolumn)
    vim.cmd.tabnew({ args = { "README.md" } })
    assert.equals("auto", vim.o.signcolumn)
  end)

  a.it("Restores window options on tabnew", function()
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.equals("no", vim.o.signcolumn)
    vim.cmd.tabnew()
    vim.cmd.edit({ args = { "README.md" } })
    assert.equals("auto", vim.o.signcolumn)
  end)

  a.it("Sets the window options when re-entering oil buffer", function()
    oil.open()
    test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
    assert.truthy(vim.w.oil_did_enter)
    vim.cmd.edit({ args = { "README.md" } })
    assert.falsy(vim.w.oil_did_enter)
    oil.open()
    assert.truthy(vim.w.oil_did_enter)
    vim.cmd.vsplit()
    assert.truthy(vim.w.oil_did_enter)
  end)
end)
